package action.util;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import action.pojo.FtpConnect;
import action.pojo.LocalFilePojo;
import android.os.Environment;
public class Tool {
	public static DateFormat format1= new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
	// Local
	public static String strlocal_path=Environment.getExternalStorageDirectory().toString();
	public static ArrayList<String> local_path=new ArrayList<String>();
	public static int position;//1:Local; 2:Lan; 3:FTP
	public static ArrayList<LocalFilePojo> list_history = new ArrayList<LocalFilePojo>();
	public static ArrayList<LocalFilePojo> list_favorite = new ArrayList<LocalFilePojo>();

	// FTP 	
	public static ArrayList<String> ftp_path = new ArrayList<String>();
	public static FTPClient client = new FTPClient();
	public static ArrayList<FtpConnect> list_favorite_ftp = new ArrayList<FtpConnect>();
	public static ArrayList<FtpConnect> list_history_ftp = new ArrayList<FtpConnect>();

	public static ArrayList<String> dest_ftp_path_popup = new ArrayList<String>();
	public static FtpConnect dest_connect_popup= new FtpConnect();
	public static String src_ftp_path="";
	public static String dest_ftp_path="";
	public static ArrayList<FtpConnect> multiSelect = new ArrayList<FtpConnect>();
	
	public static FtpConnect connect= new FtpConnect();

	// LAN
	public static ArrayList<String> lan_path = new ArrayList<String>();

	public static String locale(int key){
		String locale = "";
		switch (key) {
		case 0:
			locale = "";
			break;
		case 1:
			locale = "kr";		
			break;
		case 2:
			locale = "vi";
			break;
		default:
			break;
		}
		return locale;
	}

	public static String getLocalPath(ArrayList<String> local_path1){
		String kq="";
		if(local_path1!=null && local_path1.size()>0){
			for(String item: local_path1){
				if(kq.equals("")){
					kq =item;
				}else{
					kq +=File.separator+item;
				}
			}
		}
		return kq;
	}

	public static String getFTPPath(ArrayList<String> ftppath){
		String kq="";
		if(ftppath!=null && ftppath.size()>0){
			for(int i=0;i<ftppath.size();i++){
				if(kq.equals("")){
					kq =ftppath.get(i);
				}else{
					if(ftppath.get(i-1).equals(File.separator)){
						kq +=ftppath.get(i);
					}else{
						kq +=File.separator+ftppath.get(i);
					}
				}
			}
		}
		System.out.println("kq: "+kq);
		return kq;
	}

	public static ArrayList<LocalFilePojo> sortLocal(ArrayList<LocalFilePojo> list, int sort) {
		switch (sort) {
		case 0:
			Collections.sort(list, new Comparator<LocalFilePojo>() {

				@Override
				public int compare(LocalFilePojo object1, LocalFilePojo object2) {
					return object1.getFileItem().getName().toLowerCase().compareTo(object2.getFileItem().getName().toLowerCase());
				}
			});
			break;
		case 1:
			Collections.sort(list, new Comparator<LocalFilePojo>() {

				public int compare(LocalFilePojo object1, LocalFilePojo object2) {
					if(!object1.getFileItem().isDirectory()&&!object2.getFileItem().isDirectory())
						return String.valueOf(object1.getTotalsize()).compareTo(String.valueOf(object2.getTotalsize()));
					else{
						return object1.getFileItem().getName().toLowerCase().compareTo(object2.getFileItem().getName().toLowerCase());
					}
				}
			});
			break;
		case 2:
			Collections.sort(list, new Comparator<LocalFilePojo>() {

				public int compare(LocalFilePojo object1, LocalFilePojo object2) {
					return String.valueOf(object1.getFileItem().lastModified()).compareTo(String.valueOf(object2.getFileItem().lastModified()));
				}
			});
			break;
		}
		return list;
	}

	//tinh rieng thu muc
	public static long getTotalLocalSize(File file) {
		long result = 0;
		File[] fileList = file.listFiles();

		if(fileList!=null && fileList.length>0){
			for(int i = 0; i < fileList.length; i++) {
				if(fileList[i].isDirectory()) {
					result += getTotalLocalSize(fileList [i]);
				} else {
					result += fileList[i].length();
				}
			}
		}

		return result; 
	}

	public static boolean checkExtendFile(String filename, String extend) {
		int number = extend.length();
		if (filename.length() > number) {
			if (filename.substring(filename.length() - number).toLowerCase()
					.equals(extend.toLowerCase())) {
				return true;
			}
		}
		return false;
	}
	public static long totalSizeFTP(FTPFile ftpFile,String path){
		long result = 0;
		connects();
		try {
			String pathcha= path+File.separator + ftpFile.getName();
			FTPFile[] ftpFiles = client.listFiles(pathcha);
			for (int i = 0; i < ftpFiles.length; i++) {
				if(ftpFiles[i].isDirectory()){
					result += totalSizeFTP(ftpFiles[i], pathcha );
				}else{

					result += ftpFiles[i].getSize();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				client.disconnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public static boolean connects() {
		boolean rs = false;
		try {
			client.connect(Tool.connect.getHostname(), Tool.connect.getPort());
			client.setAutodetectUTF8(true);
			rs = client.login(Tool.connect.getUsername(), Tool.connect.getPassword());
			client.setFileType(FTP.BINARY_FILE_TYPE);
			client.enterLocalPassiveMode();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return rs;
	}

	public static boolean generalConnects(FTPClient gclient,FtpConnect gconnect) {
		boolean rs = false;
		try {
			gclient.connect(gconnect.getHostname(), gconnect.getPort());
			gclient.setAutodetectUTF8(true);
			rs = gclient.login(gconnect.getUsername(), gconnect.getPassword());
			gclient.setFileType(FTP.BINARY_FILE_TYPE);
			gclient.enterLocalPassiveMode();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return rs;
	}

	public static void generalCloseconnect(FTPClient gclient){
		try{
			gclient.logout();
			gclient.disconnect();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void closeconnect(){
		try{
			client.logout();
			client.disconnect();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	// delete ftp
	public static void dodelete(FTPFile ftpFile,String path){
		String pathcha="";
		System.out.println(" path " + path);
		try {
			if(path.equals((File.separator))){
				pathcha= path+ ftpFile.getName();
			}else
				pathcha= path+File.separator + ftpFile.getName();

			System.out.println("pathcha: "+pathcha);

			FTPFile[] ftpFiles = client.listFiles(pathcha);
			if(ftpFile.isDirectory()){

				if(ftpFiles!=null && ftpFiles.length>0){
					for (int i = 0; i < ftpFiles.length; i++) {
						dodelete(ftpFiles[i], pathcha);
					}
					client.removeDirectory(pathcha);
					System.out.println("delete thu muc: "+ftpFile.getName());
				}else{
					if(client.removeDirectory(pathcha)){
						System.out.println("delete thu muc rong: "+ftpFile.getName());
					}else
						System.out.println("loi delete thu muc rong");
				}

			}else{
				System.out.println(" delete path file == " + pathcha);
				client.deleteFile(pathcha);
				System.out.println("delete : "+ftpFile.getName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// sort ftp
	public static ArrayList<FtpConnect> sortFTP(ArrayList<FtpConnect> list, int sort) {
		switch (sort) {
		case 0:
			Collections.sort(list, new Comparator<FtpConnect>() {

				@Override
				public int compare(FtpConnect obj1, FtpConnect obj2) {
					return obj1.getFtpFile().getName().toLowerCase().compareTo(obj2.getFtpFile().getName().toLowerCase());
				}
			});
			break;
		case 1:
			Collections.sort(list, new Comparator<FtpConnect>() {

				@Override
				public int compare(FtpConnect obj1, FtpConnect obj2) {
					if((!obj1.getFtpFile().isDirectory()) && (!obj2.getFtpFile().isDirectory())){
						return String.valueOf(obj1.getTotalsize()).compareTo(String.valueOf(obj2.getTotalsize()));
					}else{
						return obj1.getFtpFile().getName().toLowerCase().compareTo(obj2.getFtpFile().getName().toLowerCase());
					}
				}
			});
			break;
		case 2:
			Collections.sort(list,new Comparator<FtpConnect>() {

				@Override
				public int compare(FtpConnect obj1, FtpConnect obj2) {
					return String.valueOf(obj1.getFtpFile().getTimestamp().getTime()).compareTo( String.valueOf(obj2.getFtpFile().getTimestamp().getTime()));
				}
			});
		default:
			break;
		}
		return list;
	}
}
